package com.czk.springcloud.controller;

import com.czk.springcloud.entity.CommonResult;
import com.czk.springcloud.entity.Payment;
import com.czk.springcloud.model.BizException;
import com.czk.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author:ChenZhangKun
 * @Date: 2020/12/16 16:57
 */
@RestController
@RequestMapping("/payment")
@Slf4j
public class PaymentController {

    @Value("${server.port}")
    private String serverPort;
    @Resource
    private DiscoveryClient discoveryClient;
    @Autowired
    private PaymentService paymentService;

    /**
     * 写
     *
     * @param payment
     * @return
     */
    @PostMapping("/insert")
    public CommonResult create(@RequestBody Payment payment) {
        int result = paymentService.create(payment);
        log.info("插入结果" + result);
        if (result > 0) {
            return new CommonResult(200, "插入成功" + serverPort, result);
        } else {
            return new CommonResult(500, "插入失败" + serverPort, result);
        }
    }

    /**
     * 读
     *
     * @param id
     * @return
     */
    @GetMapping("/select/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id) {
        Payment payment = paymentService.getPaymentById(id);
        log.info("查询结果" + payment);
        if (payment != null) {
            return new CommonResult(200, "查询成功" + serverPort, payment);
        } else {
            return new CommonResult(200, "查询失败" + serverPort, null);
        }
    }

    /**
     * 服务发现discoveryClient
     *
     * @return
     */
    @GetMapping("/payment/discovery")
    public Object discovery() {
        // 拿到所有的eureka的服务信息
        List<String> services = discoveryClient.getServices();
        for (String service : services) {
            log.info("服务清单" + service);
        }
        // 拿到具体的实例
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        for (ServiceInstance instance : instances) {
            // 拿到每个服务的服务id、主机、端口、和uri
            log.info(instance.getServiceId() + "\t" + instance.getHost() + "\t" + instance.getPort() + "\t" + instance.getUri());
        }
        return discoveryClient;
    }

    /**
     * 测试自定义负载均衡算法
     *
     * @return
     */
    @GetMapping("/lb")
    public String getPaymentLb() {
        return serverPort;
    }

    /**
     * 模拟feign调用超时
     *
     * @return
     */
    @GetMapping("/feign/timeout")
    public String paymentFeignTimeout() {
        try {
            Thread.sleep(3 * 1000);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        return serverPort;
    }

    /**
     * zipkin的应用测试
     *
     * @return
     */
    @GetMapping("/zipkin")
    public String paymentZipkin() {
        return "welcome to zipkin";
    }

    /**
     * 测试用feign调用时返回不封装数据，
     * 普通的http调用利用BaseResponseAdvice封装数据
     *
     * @return
     */
    @GetMapping("/test/feign")
    public Payment payment() {
        Payment payment = new Payment();
        payment.setSerial("dsdsds");
        payment.setId(4454L);
        return payment;
    }

    /**
     * 模拟异常捕获，直接抛出给feign客户端，不经过全局异常捕获
     * @param accountCode
     * @return
     */
    @GetMapping("/error/{accountCode}")
    public Payment selectByCode(@PathVariable String accountCode) {
        if ("czk".equals(accountCode)) {
            throw new BizException(accountCode + "用户不存在");
        }
        Payment payment = new Payment();
        payment.setSerial("56565");
        return payment;
    }
}
